<?php

namespace App\Models\CacheModel\wecaht;

use App\Models\Api\BaseCache;
use AstroTool\Cache\CacheManage;

class WechatLingqianUserCardsChangeRecode extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.wechat_lingqian_user_cards_change_recode';

    protected $cacheModel = 'wechat_h5.user_cards';

    /**
     * 获取 今日 抽牌信息
     * @param int $uid
     * @param int $date
     * @return array|null
     * @throws \Exception
     */
    public function getTodayDrawCardData(int $uid, int $date)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('today_is_draw', "{$uid}:{$date}")
            ->getDataAndSet(function () use ($uid, $date)
            {
                try
                {
                    $startTime = strtotime($date);
                    $data = $this->where(['uid' => $uid, 'type' => 1])->whereBetween('create_time', [$startTime, $startTime + 86400])->findOrFail()->toArray();

                    return $data;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, [], 'today_is_draw');

        if ($resData)
        {
            return arrayKeyData2int($resData, ['id', 'uid', 'card_id', 'change_num', 'type', 'create_time']);
        } else
        {
            return null;
        }
    }
}
